package run.newbug.jt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import run.newbug.jt.pojo.User;
import run.newbug.jt.service.UserService;
import run.newbug.jt.vo.PageResult;
import run.newbug.jt.vo.SysResult;

import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("getAll")
    public List<User> getAll(){
        return userService.getAll();
    }

    @PostMapping("login")
    public SysResult login(@RequestBody User user){
        String token = userService.login(user);
//        if(token == null || token.length() > 0) return SysResult.fail();
//        else return SysResult.succe();
        if(StringUtils.hasLength(token)) return SysResult.succe(token);
        return SysResult.fail();
    }

    @GetMapping("list")
    public SysResult list(PageResult pageResult){
        return SysResult.succe(userService.list2(pageResult));
    }

    @PutMapping("status/{id}/{status}")
    public SysResult Statu(@PathVariable Integer id, @PathVariable Boolean status){
        return SysResult.succe(userService.updaSta(id,status));
    }

    @PostMapping("addUser")
    public SysResult addUser(@RequestBody User user){
        return SysResult.succe(userService.addUSer(user));
    }

    @DeleteMapping("/{id}")
    public SysResult dele(@PathVariable Integer id){
        return SysResult.succe(userService.dele(id));
    }

    @GetMapping("/{id}")
    public SysResult updaUserGet(@PathVariable Integer id){
        return SysResult.succe(userService.updaUserGet(id));
    }

    @PutMapping("updateUser")
    public SysResult updaUser(@RequestBody User user){
        return SysResult.succe(userService.updaUser(user));
    }

}
